﻿using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FilterWebApp.Utils.Filters
{
    /// <summary>
    /// 自定义ActionFilter
    /// </summary>
    
    public class CustomActionFilterAttribute :Attribute,IActionFilter
    {
        //private readonly ILogger<CustomActionFilterAttribute> _logger;

        //public CustomActionFilterAttribute(ILogger<CustomActionFilterAttribute> logger)
        //{
        //    _logger = logger;
        //    _logger.LogInformation("正在执行CustomActionFilterAttribute构造函数");
        //}

        public CustomActionFilterAttribute()
        {
            Console.WriteLine("正在执行Action Filter 的构造函数。。。");
        }


        /// <summary>
        /// action执行后
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuted(ActionExecutedContext context)
        {
            Console.WriteLine("正在执行Action Filter 的OnActionExecuted。。。");
            //_logger.LogInformation("Log：正在执行OnActionExecuted。。。");
        }
        /// <summary>
        /// action执行前
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuting(ActionExecutingContext context)
        {
            string controllerName = context.ActionDescriptor.RouteValues["controller"];
            string actionName = context.ActionDescriptor.RouteValues["action"];
            string res = $"Controller：{controllerName}，Action：{actionName}\n{context.Result}";
            Console.WriteLine("正在执行Action Filter 的OnActionExecuting。。。");
           // _logger.LogInformation("Log：正在执行OnActionExecuting。。。");
        }
    }



    /// <summary>
    /// 自定义控制器ActionFilter
    /// </summary>

    public class CustomControllerActionFilterAttribute : Attribute, IActionFilter
    {
        

        public CustomControllerActionFilterAttribute()
        {          
            Console.WriteLine("正在执行Controller Filter的构造函数");
        }


        /// <summary>
        /// action执行后
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuted(ActionExecutedContext context)
        {
             Console.WriteLine("正在执行Controller Filter的OnActionExecuted。。。");
            
        }
        /// <summary>
        /// action执行前
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuting(ActionExecutingContext context)
        {
             Console.WriteLine("正在执行Controller Filter的OnActionExecuting。。。");
        }
    }


    /// <summary>
    /// 全局ActionFilter
    /// </summary>

    public class CustomGlobalActionFilterAttribute : Attribute, IActionFilter
    {


        public CustomGlobalActionFilterAttribute()
        {
            Console.WriteLine("正在执行Global Filter 的构造函数。。");
        }


        /// <summary>
        /// action执行后
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuted(ActionExecutedContext context)
        {
            Console.WriteLine("正在执行Global Filter 的OnActionExecuted。。。");

        }
        /// <summary>
        /// action执行前
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuting(ActionExecutingContext context)
        {
            Console.WriteLine("正在执行Global Filter 的OnActionExecuting。。。");
        }
    }

}
